<?php
	class Login extends CI_Controller {
		function login()
		{
			parent::__construct();
			@session_start();
			if (!isset($_SESSION['error_loginadmin'])) {
				$_SESSION['error_loginadmin']	= '';
			}
		}
			
		function index() {		
			$data['title'] = "Trang đăng nhập";
			$this->load->view('back_end/template_login',$data);
			
			//kiem tra xem cookie da duoc thiet lap chua
			if (isset($_COOKIE['username']) &&  isset($_COOKIE['password']) && !empty($_COOKIE['username']) &&  !empty($_COOKIE['password'])){
				//kiem tra gia tri
				$username = $_COOKIE['username'];
				$password = $_COOKIE['password'];
			
				$value_user = $this->db->get_where('admins',array('username'=> $username, 'password'=>$password, 'status'=>'1'));
				$_SESSION['admin_login'] = FALSE;	
		
				if ($value_user->num_rows()>0) {				
					$row = $value_user->row_array();
			
					$_SESSION['admin_id']			= $row['id'];
					$_SESSION['admin_login']		= TRUE;
					$_SESSION['username']			= $row['username'];	
					$_SESSION['show']				= 0;
					$_SESSION['error_loginadmin']	= '';	
					
					redirect('back_end/backend');	
					
				}else{
					$_SESSION['show']				= 1;
					$_SESSION['admin_id']			= '';
					$_SESSION['admin_login']		= FALSE;
					$_SESSION['username']			= '';
					$_SESSION['error_loginadmin']	= 'Username hoặc Password không hợp lệ!';

					redirect('back_end/login');	
				} 
			}

		}
		
		function check_login () {
			$sumit_login = $this->input->post("submit");
			$username = $this->input->post("username");
			$password = md5($this->input->post("password"));
			$pass_check = $this->input->post("pass_check");
			$_SESSION['pass_check'] = $pass_check;
			
			if(empty($sumit_login)) {
				redirect('back_end/login');
			} else {
				$this->mod_ad->login_admin($username,$password);
			}
		}
		
		function logout(){
			session_destroy();
			setcookie('username','',time() - 60); 
			setcookie('password','',time() - 60); 
			redirect('back_end/login');	
		}
		
	}
?>